﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace UpwardsTimeClock.Prototype
{
    static class Program
    {
        public static UpwardsTimeClockConfig Config { get; private set; }

        private static XmlSerializer _configSerializer;
        private static string _configFile;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                _configSerializer = new XmlSerializer(typeof(UpwardsTimeClockConfig));
                _configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                    "UpwardsTimeClock.config");

                LoadConfig();

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                string message = string.Format("An exception occurred. The program is ending.{0}{0}{1}",
                    Environment.NewLine, ex.Message);
                MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, 
                    MessageBoxIcon.Exclamation);
            }
        }

        public static void LoadConfig()
        {
            using (Stream stream = new FileStream(_configFile, FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                Config = _configSerializer.Deserialize(stream) as UpwardsTimeClockConfig;
            }
        }

        public static TimeSpan GetScaledTimeSpan(TimeSpan timeSpan)
        {
            return TimeSpan.FromMilliseconds(timeSpan.TotalMilliseconds * Config.TimeScale);
        }

        public static TimeSpan GetScaledDisplayTimeSpan(TimeSpan timeSpan)
        {
            return TimeSpan.FromMilliseconds(timeSpan.TotalMilliseconds / Config.TimeScale);
        }

        public static TimeSpan GetScaledTimeSpan(string timeSpanString)
        {
            return GetScaledTimeSpan(TimeSpan.Parse(timeSpanString));
        }

        public static string BuildFilePath(string basePath, string file)
        {
            string returnValue = null;
            try
            {
                string hourString = DateTime.Now.ToString("HH");
                string dateString = DateTime.Now.ToString("MM-dd");
                string fileName = file.Replace("HOUR$", hourString).Replace("DATE$", dateString);
                string fullPath = Path.Combine(basePath, fileName);
                if (fullPath.Contains("*") || fullPath.Contains("?"))
                {
                    string pathPart = Path.GetDirectoryName(fullPath);
                    string filePart = Path.GetFileName(fullPath);
                    string[] files = Directory.GetFiles(pathPart, filePart);
                    if (files != null && files.Length > 0)
                    {
                        returnValue = files[0];
                    }
                }
                else if (File.Exists(fullPath))
                {
                    returnValue = fullPath;
                }
            } catch { }
            return returnValue;
        }

    }
}
